$(function(){

    var $disabled = $('.keywords_disabled');

    $disabled.click(function(){
        var txt = $(this).html();
        var disabled = 0;
        if(txt == '启用'){
            disabled = 1;
        }else if(txt == '禁用'){
            disabled = 0;
        }else{
            return false;
        }

        var id = GetPostfix($(this).attr('id'));
        var params = {
            'id':id,
            'disabled':disabled
        };

        var $span = $(this);

        $.post('/index.php/Admin/Keywords/update_disabled',params,function(data){
            if(data.success){

                if(disabled == 0){
                    $span.removeClass('color-red').addClass('color-green').html('启用');
                }else if(disabled == 1){
                    $span.removeClass('color-green').addClass('color-red').html('禁用');
                }

            }else{
                alert(data.message);
            }
        },'json');

    });



    var $btnSearch = $('#btnSearch');

    $btnSearch.click(function(){
        goSearch();

    });




    //全选
    var $check_all = $("#check_all");
    var $check_orders = $("input[name='check_keywords']");

    $check_all.click(function(){

        var checked = false;
        if($(this).is(':checked')){
            checked = true;
        }

        $check_orders.each(function(i){
            var $check = $($check_orders[i]);

            if(checked){
                if(!$check.is(':checked')){
                    $check.click();
                }
            }else{
                if($check.is(':checked')){
                    $check.click();
                }
            }
        });

    });


});

//随机排序
function URL_Random_Sort(){

    if(!confirm('确认要重新随机排序?')){
        return false;
    }

	$.post('/index.php/Admin/Keywords/random_sort',{},function(data){
		if(data.success){
			alert(data.message);
			location.reload(true);
		}else{
			alert(data.message);
		}
	},'json');
}


function goSearch(){

    var search 		= $('#search').val();
    var keywords 	= $.trim($('#keywords').val());
    var plantform_id 		= $('#plantform_id').val();

    var params = {
        'search'	:	search,
        'keywords'	:	keywords,
        'plantform_id':plantform_id
    }

    location.href = '/index.php/Admin/Keywords?' + $.param(params);
}


function keywordsDisabled(disabled){

    var $check_keywords = $("input[name='check_keywords']:checked");

    if($check_keywords.length == 0){
        alert('请先选择URL');
        return false;
    }

    if(!confirm('确认继续操作?')){
        return false;
    }

    var keywords = new Array();
    $check_keywords.each(function(i){
        var keywords_id = $($check_keywords[i]).val();
        keywords.push(keywords_id);
    });

    $.post('/index.php/Admin/Keywords/batch_update_disabled',{
        'keywords':keywords.toString(),
        'disabled':disabled
    },function(data){
        if(data.success){
            alert(data.message);
            location.reload(true);
        }else{
            alert(data.message);
        }
    },'JSON');
}